﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryDrug : ADONetRepositoryBase<Drug>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Drug entity)
        {
            var cmd = new SqlCommand("DrugInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Name", entity.DrugName));
            logger.Trace("SqlCommand для записи данных Препарата в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("DrugDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Препарата id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Drug entity)
        {
            var cmd = new SqlCommand("DrugUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Name", entity.DrugName));
            logger.Trace("SqlCommand для обновления данных Препарата id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetDrug", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Препарата id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetDrugByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Препарата {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Drug GetEntity(SqlDataReader sqlDataReader)
        {
            Drug drug = new Drug();
            drug.Id = (int)sqlDataReader["Id"];
            drug.DrugName = sqlDataReader["Name"].ToString();
            return drug;
        }
    }
}
